package com.demo.springboot;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @GetMapping ---- spring4.3中的新特性
 * @ResponseBody  表示当前的方法的返回值可以直接作为一个body返回展示
 */
@Controller
public class UserController {

    @RequestMapping(value = "/user/home", method = RequestMethod.GET)
    @ResponseBody
    public String home() {
        return "User Home";
    }

    @ResponseBody
    @GetMapping(value = "/user/show/{id}")
    public String show(@PathVariable("id") String id) {
        return "User Show,id=" + id;
    }

    @ResponseBody
    @PostMapping(value = "/user/create")
    public String create(@RequestParam("username") String usrName,
                         @RequestParam(value = "password", required = false, defaultValue = "root456") String pwd) {
        return "User Create: userName:" + usrName + "\t password: " + pwd;
    }

    @ResponseBody
    @GetMapping("/user/ip")
    public String host(HttpServletRequest request){
        return "Server Host IP:"+request.getRemoteAddr();
    }
}
